package com.xindaoapp.happypet.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.CommonViewPagerAdapter;
import com.xindaoapp.happypet.fragments.DuihuanDetailFragment;
import com.xindaoapp.happypet.fragments.WeibaDetailFragment;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.view.CircleImageView;

/**
 * 我的记录+尾巴 最新
 * 
 * @author Stone
 * 
 */
public class MyRecordActivity extends BaseActivity {
    private ViewPager vViewPager;
    private boolean key_bundle;
    private CircleImageView vPhoto;
    private TextView vDuihuan, vName, vJinbi;
    private WeiBaCountChangedReceiver mWeiBaCountChangedReceiver;


    @Override
    protected int getContentViewResId() {
        return R.layout.activity_my_record;
    }

    @Override
    protected void initViews() {
        super.initViews();

        mWeiBaCountChangedReceiver = new WeiBaCountChangedReceiver();
        registerReceiver(mWeiBaCountChangedReceiver, new IntentFilter(
                Constants.ACTION_WEIBA_COUNT_CHANGED));

        key_bundle = getIntent().getBooleanExtra("isPsersonal", false);
        vPhoto = (CircleImageView) findViewById(R.id.pic);
        vDuihuan = (TextView) findViewById(R.id.duihuan);
        vName = (TextView) findViewById(R.id.name);
        vJinbi = (TextView) findViewById(R.id.jinbi);

        vViewPager = (ViewPager) findViewById(R.id.viewpager);
        Fragment[] fragments =
                new Fragment[] {WeibaDetailFragment.newInstance(),
                        DuihuanDetailFragment.newInstance()};
        View[] tabViews = new View[] {findViewById(R.id.tab_1), findViewById(R.id.tab_2)};
        CommonViewPagerAdapter adapter =
                new CommonViewPagerAdapter(getSupportFragmentManager(), fragments, tabViews);
        for (int i = 0; i < tabViews.length; i++) {
            final int temp = i;
            tabViews[i].setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    vViewPager.setCurrentItem(temp);
                }
            });
        }
        vViewPager.setAdapter(adapter);
        vViewPager.setOffscreenPageLimit(2);
        vViewPager.setOnPageChangeListener(adapter);

        iniUserInfo();
    }



    private void iniUserInfo() {
        // TODO Auto-generated method stub

        String jinbi_text = getResources().getString(R.string.weibatext);
        if (TextUtils.isEmpty(UserState.getUser().userface)) {
            ImageLoader.getInstance().displayImage(UserState.getUser().userface, vPhoto);
        }
        if (!TextUtils.isEmpty(UserState.getUser().username)) {
            vName.setText(UserState.getUser().username);
        }
        vJinbi.setText(String.format(jinbi_text, UserState.getUser().niuniu));

        vDuihuan.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (key_bundle) {
                    Intent intent = new Intent(MyRecordActivity.this, ChangeCenterActivity.class);
                    startActivity(intent);
                } else {
                    MyRecordActivity.this.finish();
                }
            }
        });

    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        };
    }

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }

    @Override
    protected String getTopBarTitleStrRes() {
        return "我的尾巴币";
    }

    @Override
    protected OnClickListener getRightViewClickListener() {
        // TODO Auto-generated method stub
        return new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(getApplicationContext(), WebViewActivity.class);
                intent.putExtra(Constants.KEY_TOP_BAR_TITLE, "尾巴币规则");
                intent.putExtra(Constants.KEY_VIDEO_URL, Constants.VALUE_URL);
                startActivity(intent);

            }
        };
    }

    @Override
    protected int getTopBarRightTextViewRes() {
        // TODO Auto-generated method stub
        return R.string.title_guize;
    }


    class WeiBaCountChangedReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            iniUserInfo();
        }

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unregisterReceiver(mWeiBaCountChangedReceiver);
    }

}
